
package com.av.torrent.helpers;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

/**
 * @author Uladzimir_Kerus
 */
public class PreferencesHelper {

  private final static String DEFAULT_HOST = "localhost";

  private final static String DEFAULT_PORT = "9091";

  private final static boolean DEFAULT_AUTHORIZATION_ENABLED = false;

  private final static String HOST_KEY = "hostname";

  private final static String PORT_KEY = "port";

  private final static String USERNAME_KEY = "username";

  private final static String PASSWORD_KEY = "password";

  private final static String REQUIRE_AUTHORIZATION_KEY = "authentication_enabled";

  private String mHost;

  private int mPort;

  private boolean mAuthorizationEnabled;

  private String mUserName;

  private String mPassword;

  private final OnSharedPreferenceChangeListener mOnPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      if (HOST_KEY.equals(key)) {
        mHost = sharedPreferences.getString(HOST_KEY, DEFAULT_HOST);
      } else if (PORT_KEY.equals(key)) {
        mPort = Integer.valueOf(sharedPreferences.getString(PORT_KEY, DEFAULT_PORT));
      } else if (REQUIRE_AUTHORIZATION_KEY.equals(key)) {
        mAuthorizationEnabled = sharedPreferences.getBoolean(REQUIRE_AUTHORIZATION_KEY, DEFAULT_AUTHORIZATION_ENABLED);
      } else if (USERNAME_KEY.equals(key)) {
        mUserName = sharedPreferences.getString(USERNAME_KEY, "");
      } else if (PASSWORD_KEY.equals(key)) {
        mPassword = sharedPreferences.getString(PASSWORD_KEY, "");
      }
    }
  };

  /**
   * @param context
   */
  public PreferencesHelper(final Context context) {
    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    sharedPreferences.registerOnSharedPreferenceChangeListener(mOnPreferenceChangeListener);
    // initialization of properties values
    mHost = sharedPreferences.getString(HOST_KEY, DEFAULT_HOST);
    mPort = Integer.valueOf(sharedPreferences.getString(PORT_KEY, DEFAULT_PORT));
    mAuthorizationEnabled = sharedPreferences.getBoolean(REQUIRE_AUTHORIZATION_KEY, DEFAULT_AUTHORIZATION_ENABLED);
    mUserName = sharedPreferences.getString(USERNAME_KEY, "");
    mPassword = sharedPreferences.getString(PASSWORD_KEY, "");
  }

  /**
   * @return the mHost
   */
  public String getHost() {
    return mHost;
  }

  /**
   * @return the mPort
   */
  public int getPort() {
    return mPort;
  }

  /**
   * @return the mAuthorizationEnabled
   */
  public boolean isAuthorizationEnabled() {
    return mAuthorizationEnabled;
  }

  /**
   * @return the mUserName
   */
  public String getUserName() {
    return mUserName;
  }

  /**
   * @return the mPassword
   */
  public String getPassword() {
    return mPassword;
  }
}
